package com.sms.jiujia.ui;

import com.sms.jiujia.ui.setting.Constant;
import com.sms.jiujia.ui.setting.HomeWindowSetting;
import com.sms.jiujia.ui.setting.PositionSetting;
import com.sms.jiujia.ui.setting.Setting;
import com.sms.jiujia.utils.DataUtil;
import com.sms.jiujia.utils.TrayWindowUtil;
import com.sms.jiujia.utils.UtilTools;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextArea;
import javafx.scene.layout.Pane;
import javafx.scene.text.Text;
import javafx.stage.Stage;

import java.io.IOException;
import java.io.InputStream;
import java.net.URL;

import static com.sms.jiujia.utils.UtilTools.parseCitiesFromTextFile;
import static com.sms.jiujia.utils.UtilTools.parseCitiesFromTextFile2;

/**
 * @Author songmingsong
 * @Date 2023/4/28
 **/
public class Home {

    public static Pane homeUi(Stage primaryStage) throws IOException {
        primaryStage.setTitle("疫苗信息推送@songmingsong");
        /**
         * 初始化窗体
         */
        HomeWindowSetting.set(primaryStage);
        Setting setting = new Setting();
        setting.setBtnTest(new Button("测试"));
        setting.setBtnStart(new Button("开始"));
        setting.setReSetKeyBtn(new Button("重新输入KEY"));
        setting.setChoiceBoxTitle(new Text("城市："));
        setting.setSetPushDeerKeyBtn(new Button("输入PushDeerKey"));
        setting.setTextArea(new TextArea("开始输出日志...\n"));
        //setting.setTitle(new Label("疫苗信息推送@songmingsong"));
        setting.setGetKeyMethodBtn(new Button("查看"));
        setting.setStopBtn(new Button("停止"));
        setting.setNotice(new Text("使用说明：\n" +
                "1、第一次启动应用会默认置于系统底部托盘中\n" +
                "2、使用时需要申请pushDeer的key，申请方法请点击底部【查看】按钮\n" +
                "3、请选择需要查询推送的城市，如不选择，默认推送成都九价疫苗信息\n" +
                "4、点击程序“x”按钮不会关闭程序，如需打开/关闭程序请在系统托盘中找到绿色喇叭图标，点击鼠标右键选择打开或者退出\n" +
                "5、程序退出后将不再提供推送服务"));

        /**
         * 初始化城市代码
         */
        //parseCitiesFromTextFile(TrayWindowUtil.class.getResource("/data/city.txt"));
        InputStream resourceAsStream = Home.class.getClassLoader().getResourceAsStream("city.txt");
        //parseCitiesFromTextFile(Home.class.getClassLoader().getResource("city.txt").getFile());
        parseCitiesFromTextFile2(resourceAsStream);
        setting.getCityBox().getItems().addAll(Constant.cities);


        /**
         * 设置组件位置
         */
        PositionSetting.set(setting);
        /*
         * 将节点添加到面板
         * */
        Pane pane = UtilTools.getNewPane();
        pane.getChildren().add(setting.getBtnTest());
        pane.getChildren().add(setting.getBtnStart());
        pane.getChildren().add(setting.getTextArea());
        pane.getChildren().add(setting.getCityBox());
        pane.getChildren().add(setting.getNotice());
        pane.getChildren().add(setting.getChoiceBoxTitle());
        pane.getChildren().add(setting.getGetKeyMethodBtn());
        pane.getChildren().add(setting.getStopBtn());
        if (!Constant.has_PUSH_DEER_KEY) {
            pane.getChildren().add(setting.getSetPushDeerKeyBtn());
        }

        Scene scene = new Scene(pane);
        primaryStage.setScene(scene);
        primaryStage.show();
        // 将窗体赋值给全局变量
        Setting.homeStage = primaryStage;
        Constant.setting = setting;

        return pane;

    }
}
